package com.hualan.sfzy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hualan.sfzy.constant.CommonConstant;
import com.hualan.sfzy.mapper.CourseVideoMapper;
import com.hualan.sfzy.model.entity.Course;
import com.hualan.sfzy.model.entity.CourseVideo;
import com.hualan.sfzy.service.CourseService;
import com.hualan.sfzy.service.CourseVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class CourseVideoServiceImpl implements CourseVideoService {
    @Autowired
    CourseVideoMapper courseVideoMapper;
    @Autowired
    CourseService courseService;
    @Override
    public int add(CourseVideo entity) {

//        增加课程总数量和课程总时长
        Long num=entity.getVideoTime();
        Course course=new Course();
        course.setCourseTime(num);
        course.setVideoNumber(1);
        course.setId(entity.getCourseId());
        int result=courseService.edit(course);
        return courseVideoMapper.insert(entity);
    }

    @Override
    public int edit(CourseVideo entity) {
        CourseVideo courseVideo=courseVideoMapper.selectById(entity.getId());
        Long beforeNum=courseVideo.getVideoTime();
        Long afterNum=entity.getVideoTime();
        Long num=afterNum-beforeNum;
        Course course=new Course();
        course.setCourseTime(num);
        course.setId(entity.getCourseId());
        int result=courseService.edit(course);
        return courseVideoMapper.update(entity);
    }

    @Override
    public int remove(Integer id) {
        CourseVideo courseVideo=courseVideoMapper.selectById(id);
        Course course=new Course();
        course.setCourseTime(-courseVideo.getVideoTime());
        course.setVideoNumber(-1);
        course.setId(courseVideo.getCourseId());
        int result=courseService.edit(course);
        return courseVideoMapper.delete(id);
    }

    @Override
    public int removeBatch(Integer[] ids) {

        return courseVideoMapper.deleteBatch(ids);
    }

    @Override
    public List<CourseVideo> find() {
        return courseVideoMapper.select(new CourseVideo());
    }

    @Override
    public Map<String, Object> findPage(Integer pageNum, Integer pageSize, CourseVideo entity) {
        PageHelper.startPage(pageNum, pageSize);
        List<CourseVideo> list=courseVideoMapper.select(entity);
        PageInfo<CourseVideo> pageInfo=new PageInfo<>(list);

        Map<String, Object>  resultMap = new HashMap<>();

        resultMap.put(CommonConstant.PAGE_LIST, list);    // 当前页数据列表
        resultMap.put(CommonConstant.TOTAL, pageInfo.getTotal());      // 总记录数
        resultMap.put(CommonConstant.PAGE_NUM, pageInfo.getPageNum()); // 当前页码
        resultMap.put(CommonConstant.PAGE_SIZE, pageInfo.getPageSize()); // 每页大小
        resultMap.put(CommonConstant.PAGE_TOTAL, pageInfo.getPages());  // 总页数

        return resultMap;
    }

    @Override
    public CourseVideo findById(Integer id) {
        return courseVideoMapper.selectById(id);
    }

    @Override
    public List<CourseVideo> findByCourseId(int courseId) {
        CourseVideo courseVideo = new CourseVideo();
        courseVideo.setCourseId(courseId);
        List<CourseVideo> list = courseVideoMapper.select(courseVideo);


        return list;
    }
}
